#include<vector>
using namespace std;

class Solution {
public:
    bool verifyPostorder(vector<int>& postorder) {
        return dfs(postorder, 0, postorder.size() - 1);

    }
    bool dfs(vector<int>& postorder, int start, int end){
        if(start>end)return true;

    int root = postorder[end];
    int left = start;
    while(postorder[left]<root)left++;
    int m = left;
    while(postorder[left]>root)left++;

    return left==end && dfs(postorder, start, m-1) && dfs(postorder, m, end-1);
    }
};